package com.work.saturn.assets;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.utils.FullyLinearLayoutManager;
import com.work.saturn.R;
import com.work.saturn.account.AccountDetailActivity;
import com.work.saturn.bean.UserMsgDataBean;
import com.immo.libcommon.constant.RouterUtil;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.TimeUtil.stampToDate;

/**
 * @author Administrator
 * @content 账单详情
 * @date 2018/7/11
 */
@Route(path = RouterUtil.ASSETS_DETAIL_PAGE)
public class AssetsDetailActivity extends BaseHeadActivity {
    /**
     * 账单详情的列表
     */
    @BindView(R.id.assets_detail_list)
    RecyclerView mList;
    /**
     * 历史记录列表
     */
    private List<UserMsgDataBean.HistoryBean> mHistoryBeanList;
    /**
     * 用户Id
     */
    private String userId;
    /**
     * 单位
     */
    private String symbol;
    /**
     * 数量
     */
    private String num;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets_detail);
        ButterKnife.bind(this);
        setTitle(getIntent().getStringExtra("name"));
        setTitleColor(R.color.colorYellow);
        setTitleImg(R.mipmap.headimg);
        mHistoryBeanList = (List<UserMsgDataBean.HistoryBean>) getIntent().getSerializableExtra("history");
        userId = getIntent().getStringExtra("userId");
        symbol = getIntent().getStringExtra("symbol");
        num = getIntent().getStringExtra("num");
        initData();
    }

    private void initData() {
        FullyLinearLayoutManager linearLayoutManagerTheme = new FullyLinearLayoutManager(this);
        View view = LayoutInflater.from(this).inflate(R.layout.assets_head, new LinearLayout(this), false);
        ((TextView) view.findViewById(R.id.assets_head)).setText(num);
        ((TextView) view.findViewById(R.id.assets_dw)).setText(symbol);
        linearLayoutManagerTheme.setOrientation(LinearLayoutManager.VERTICAL);
        mList.setLayoutManager(linearLayoutManagerTheme);
        AssetsHistoryDetailAdapter releaseDetailAdapter = new AssetsHistoryDetailAdapter();
        releaseDetailAdapter.addHeaderView(view);
        //设置适配器
        releaseDetailAdapter.bindToRecyclerView(mList);
    }

    @OnClick({R.id.assets_detail_in, R.id.assets_detail_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.assets_detail_in:
                //转入
                ARouter.getInstance().build(RouterUtil.ASSETS_TURN_IN).withString("userId", userId).navigation();
                break;
            case R.id.assets_detail_out:
                //转出
                ARouter.getInstance().build(RouterUtil.ASSETS_TURN_OUT).withString("userId", userId)
                        .withString("name", getIntent().getStringExtra("symbol")).navigation();
                break;
        }
    }

    /**
     * 资产历史列表
     */
    private class AssetsHistoryDetailAdapter extends BaseQuickAdapter<UserMsgDataBean.HistoryBean, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener{
        AssetsHistoryDetailAdapter() {
            super(R.layout.item_assets_detail, mHistoryBeanList);
            setOnItemClickListener(this);
        }

        @Override
        protected void convert(BaseViewHolder helper, UserMsgDataBean.HistoryBean item) {
            helper.setImageResource(R.id.assets_type, userId.equals(item.getTo()) ? R.mipmap.add : R.mipmap.minus);
            helper.setText(R.id.assets_id, userId.equals(item.getTo()) ? item.getFrom() : item.getTo());
            helper.setText(R.id.assets_gas,userId.equals(item.getTo())?"":(item.getGas()+symbol));
            helper.setText(R.id.assets_time, stampToDate(item.getTimestamp()));
            helper.setText(R.id.assets_num, userId.equals(item.getTo()) ? ("+" + item.getAmount() + "  " + symbol) : ("-" + item.getAmount() + "  " + symbol));
            helper.setTextColor(R.id.assets_num, userId.equals(item.getTo()) ? getResources().getColor(R.color.colorYellow) : getResources().getColor(R.color.colorRed));
        }

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Intent intent=new Intent(AssetsDetailActivity.this, AccountDetailActivity.class);
            intent.putExtra("count", mHistoryBeanList.get(position).getAmount());
            intent.putExtra("from",mHistoryBeanList.get(position).getFrom());
            intent.putExtra("to",mHistoryBeanList.get(position).getTo());
            intent.putExtra("gas",mHistoryBeanList.get(position).getGas());
            intent.putExtra("symbol",symbol);
            intent.putExtra("desc",mHistoryBeanList.get(position).getDescription());
            intent.putExtra("num",mHistoryBeanList.get(position).getTxId());
            intent.putExtra("time", stampToDate(mHistoryBeanList.get(position).getTimestamp()));
            startActivity(intent);

        }
    }
}
